﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace PhuLeBooze.Global
{
    public class CSecurity : CHttpContext
    {
        /// <summary>
        /// ADMIN
        /// </summary>
       
        public static int USER_ID
        {
            get
            {
                int user_id = 0;
                if (Session != null && Session["USER_ID"] != null)
                {
                    user_id = Convert.ToInt32(Session["USER_ID"].ToString());
                }
                else if (CCookies.GetCookies("USER_ID") != null)
                {
                    user_id = Convert.ToInt32(CCookies.GetCookies("USER_ID"));
                }
                return user_id;
            }
            set
            {
                if (Session != null)
                {
                    Session["USER_ID"] = value;
                }
            }
        }

        public static int USER_TYPE
        {
            get
            {
                int user_type = 0;
                if (Session != null && Session["USER_TYPE"] != null)
                {
                    user_type = Convert.ToInt32(Session["USER_TYPE"].ToString());
                }
                else if (CCookies.GetCookies("USER_TYPE") != null)
                {
                    user_type = Convert.ToInt32(CCookies.GetCookies("USER_TYPE"));
                }
                return user_type;
            }
            set
            {
                if (Session != null)
                {
                    Session["USER_TYPE"] = value;
                }
            }
        }

        public static string SECRECT_TOKEN
        {
            get
            {
                string SECRECT_TOKEN = System.Guid.NewGuid().ToString();
                if (Session != null && Session["SECRECT_TOKEN"] != null)
                {
                    SECRECT_TOKEN = Session["SECRECT_TOKEN"].ToString();
                }
                else if (CCookies.GetCookies("SECRECT_TOKEN") != null)
                {
                    SECRECT_TOKEN = CCookies.GetCookies("SECRECT_TOKEN");
                }
                return SECRECT_TOKEN;
            }
            set
            {
                Session["SECRECT_TOKEN"] = value;
                CCookies.SetCookies("SECRECT_TOKEN", value);
            }
        }

        public static string USER_NAME
        {
            get
            {
                if (HttpContext.Current == null || Session == null || Session["USER_NAME"] == null)
                    return string.Empty;
                return Session["USER_NAME"].ToString();
            }
            set
            {
                if (HttpContext.Current == null || Session == null)
                {
                }

                Session["USER_NAME"] = value;
            }
        }

        public static string USER_FIRSTNAME
        {
            get
            {
                if (HttpContext.Current == null || Session == null || Session["USER_FIRSTNAME"] == null)
                    return string.Empty;
                return Session["USER_FIRSTNAME"].ToString();
            }
            set
            {
                if (HttpContext.Current == null || Session == null)
                {
                }
                Session["USER_FIRSTNAME"] = value;
            }
        }

        public static string USER_LASTNAME
        {
            get
            {
                if (HttpContext.Current == null || Session == null || Session["USER_LASTNAME"] == null)
                    return string.Empty;
                return Session["USER_LASTNAME"].ToString();
            }
            set
            {
                if (HttpContext.Current == null || Session == null)
                {
                }
                Session["USER_LASTNAME"] = value;
            }
        }
        public static bool IS_SUPER_ADMIN
        {
            get
            {
                bool IS_SUPER_ADMIN = false;
                if (HttpContext.Current != null)
                {
                    if (Session != null && Session["IS_SUPER_ADMIN"] != null)
                    {
                        IS_SUPER_ADMIN = Convert.ToBoolean(Session["IS_SUPER_ADMIN"].ToString());
                    }
                }
                return IS_SUPER_ADMIN;
            }
            set
            {
                Session["IS_SUPER_ADMIN"] = value;
            }
        }
        public static bool IS_HOST_ADMIN
        {
            get
            {
                bool IS_HOST_ADMIN = false;
                if (HttpContext.Current != null)
                {
                    if (Session != null && Session["IS_HOST_ADMIN"] != null)
                    {
                        IS_HOST_ADMIN = Convert.ToBoolean(Session["IS_HOST_ADMIN"].ToString());
                    }
                }
                return IS_HOST_ADMIN;
            }
            set
            {
                Session["IS_HOST_ADMIN"] = value;
            }
        }
        public static bool IS_ADMIN
        {
            get
            {
                bool IS_ADMIN = false;
                if (HttpContext.Current != null)
                {
                    if (Session != null && Session["IS_ADMIN"] != null)
                    {
                        IS_ADMIN = Convert.ToBoolean(Session["IS_ADMIN"].ToString());
                    }
                }
                return IS_ADMIN;
            }
            set
            {
                Session["IS_ADMIN"] = value;
            }
        }

        public static bool IsAuthenticatedAdminPage()
        {
           
            return (CSecurity.USER_ID != 0);
        }

        /// <summary>
        /// MEMBER
        /// </summary>
        /// 

        public static bool IS_REMEMBER
        {
            get
            {
                bool IS_REMEMBER = false;
                if (HttpContext.Current != null)
                {
                    if (CCookies.GetCookies("IS_REMEMBER") != null)
                    {
                        IS_REMEMBER = Convert.ToBoolean(CCookies.GetCookies("IS_REMEMBER"));
                    }
                }
                return IS_REMEMBER;
            }
            set
            {
                if (HttpContext.Current != null)
                {
                    CCookies.SetCookies("IS_REMEMBER", value.ToString());
                }
            }
        }
        public static bool IS_MEMBER
        {
            get
            {
                bool IS_MEMBER = false;
                if (HttpContext.Current != null)
                {
                    if (Session != null && Session["IS_MEMBER"] != null)
                    {
                        IS_MEMBER = Convert.ToBoolean(Session["IS_MEMBER"].ToString());
                    }
                    else if (CCookies.GetCookies("IS_MEMBER") != null)
                    {
                        IS_MEMBER = Convert.ToBoolean(CCookies.GetCookies("IS_MEMBER"));
                    }
                }
                return IS_MEMBER;
            }
            set
            {
                if (Session != null)
                {
                    Session["IS_MEMBER"] = value;
                    if (CSecurity.IS_REMEMBER)
                    {
                        CCookies.SetCookies("IS_MEMBER", value.ToString());
                    }

                }
            }
        }

        /*
        public static int MEMBER_ID
        {
            get
            {
                int MEMBERID = 0;
                if (HttpContext.Current != null)
                {
                    if (Session != null && Session["MEMBER_ID"] != null)
                    {
                        MEMBERID = Convert.ToInt32(Session["MEMBER_ID"].ToString());
                    }
                    else if (CCookies.GetCookies("ASIXPAGEID") != null)
                    {
                        MEMBERID = Convert.ToInt32(CCookies.GetCookies("ASIXPAGEID"));
                    }
                }
                return MEMBERID;
            }
            set
            {
                if (Session != null)
                {
                    Session["MEMBER_ID"] = value;                   
                }
                CCookies.SetCookies("ASIXPAGEID", value.ToString());
            }
        }

        public static string MEMBER_SECRECT_TOKEN
        {
            get
            {
                //Guid MEMBER_SECRECT_TOKEN = Guid.Empty;
                string TOKEN = "";
                if (HttpContext.Current != null)
                {
                    if (Session != null && Session["MEMBER_SECRECT_TOKEN"] != null)
                    {
                        TOKEN = Session["MEMBER_SECRECT_TOKEN"].ToString();
                    }
                    else if (CCookies.GetCookies("ASPXIDTOKEN") != null)
                    {
                        TOKEN = CCookies.GetCookies("ASPXIDTOKEN");
                    }
                }
                return TOKEN;
              
            }
            set
            {
                Session["MEMBER_SECRECT_TOKEN"] = value;
                CCookies.SetCookies("ASPXIDTOKEN", value.ToString());
              
            }
        }

        public static string MEMBER_AVATAR
        {
            get
            {
                string AVATAR = "";
                if (HttpContext.Current != null)
                {
                    if (Session != null && Session["MEMBER_AVATAR"] != null)
                    {
                        AVATAR = Session["MEMBER_AVATAR"].ToString();
                    }
                    else if (CCookies.GetCookies("MEMBER_AVATAR") != null)
                    {
                        AVATAR = CCookies.GetCookies("MEMBER_AVATAR").ToString();
                    }
                }
                return AVATAR;
            }
            set
            {
                Session["MEMBER_AVATAR"] = value;
                if (CSecurity.IS_REMEMBER)
                {
                    CCookies.SetCookies("MEMBER_AVATAR", value.ToString());
                }
            }
        }

        public static string MEMBER_EMAIL
        {
            get
            {
                string EMAIL = "";
                if (HttpContext.Current != null)
                {
                    if (Session != null && Session["MEMBER_EMAIL"] != null)
                    {
                        EMAIL = Session["MEMBER_EMAIL"].ToString();
                    }
                    else if (CCookies.GetCookies("MEMBER_EMAIL") != null)
                    {
                        EMAIL = CCookies.GetCookies("MEMBER_EMAIL").ToString();
                    }
                }
                return EMAIL;
            }
            set
            {
                Session["MEMBER_EMAIL"] = value;
                if (CSecurity.IS_REMEMBER)
                {
                    CCookies.SetCookies("MEMBER_EMAIL", value.ToString());
                }
            }
        }

        public static string MEMBER_FIRSTNAME
        {
            get
            {
                string FIRSTNAME = "";
                if (HttpContext.Current != null)
                {
                    if (Session != null && Session["MEMBER_FIRSTNAME"] != null)
                    {
                        FIRSTNAME = Session["MEMBER_FIRSTNAME"].ToString();
                    }
                    else if (CCookies.GetCookies("MEMBER_FIRSTNAME") != null)
                    {
                        FIRSTNAME = CCookies.GetCookies("MEMBER_FIRSTNAME").ToString();
                    }
                }
                return FIRSTNAME;
            }
            set
            {
                Session["MEMBER_FIRSTNAME"] = value;
                if (CSecurity.IS_REMEMBER)
                {
                    CCookies.SetCookies("MEMBER_FIRSTNAME", value.ToString());
                }
            }
        }

        public static string MEMBER_LASTNAME
        {
            get
            {
                string LASTNAME = "";
                if (HttpContext.Current != null)
                {
                    if (Session != null && Session["MEMBER_LASTNAME"] != null)
                    {
                        LASTNAME = Session["MEMBER_LASTNAME"].ToString();
                    }
                    else if (CCookies.GetCookies("LASTNAME") != null)
                    {
                        LASTNAME = CCookies.GetCookies("LASTNAME").ToString();
                    }
                }
                return LASTNAME;
            }
            set
            {
                Session["MEMBER_LASTNAME"] = value;               
                    CCookies.SetCookies("LASTNAME", value.ToString());
            }
        }
           public static bool IsAuthenticated()
        {
            return (CSecurity.MEMBER_ID != 0);
        }
        */


        public static string CAPTCHA_CODE
        {
            get
            {
                if (HttpContext.Current == null || Session == null || Session["CAPTCHA_CODE"] == null)
                    return string.Empty;
                return Session["CAPTCHA_CODE"].ToString();
            }
            set
            {
                Session["CAPTCHA_CODE"] = value;
            }
        }

       

        /// <summary>
        /// take any string and encrypt it using MD5 then
        /// return the encrypted data 
        /// </summary>
        /// <param name="data">input text you will enterd to encrypt it</param>
        /// <returns>return the encrypted text as hexadecimal string</returns>

        public static string GetMD5HashData(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();

        }

        /// <summary>
        /// encrypt input text using MD5 and compare it with
        /// the stored encrypted text
        /// </summary>
        /// <param name="inputData">input text you will enterd to encrypt it</param>
        /// <param name="storedHashData">the encrypted text
        ///         stored on file or database ... etc</param>
        /// <returns>true or false depending on input validation</returns>

        public static bool ValidateMD5HashData(string inputData, string storedHashData)
        {
            //hash input text and save it string variable
            string getHashInputData = GetMD5HashData(inputData);
            if (string.Compare(getHashInputData, storedHashData) == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}